package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.interactive.InteractiveRecord;
import com.yanqu.road.entity.interactive.UserInteractive;
import com.yanqu.road.entity.player.*;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

public class PlayerBasePb {
	public static PlayerProto.PlayerBaseTempMsg.Builder parsePlayerBaseTempMsg(long userAliasId, String nickName, int titleId, String decoration,
			String unionName, String unionUid, long unionAliasId, long vipExp, int sex, BigInteger earnSpeed, long registerTime, long serverId,
			String world, String continent, String country, String province, String city, long lastLoginTime, String badge){
		PlayerProto.PlayerBaseTempMsg.Builder msg = PlayerProto.PlayerBaseTempMsg.newBuilder();
		msg.setUserAliasId(userAliasId);
		msg.setNickName(nickName);
		msg.setTitleId(titleId);
		msg.setDecoration(decoration);
		msg.setUnionName(unionName);
		msg.setUnionUid(unionUid);
		if(unionAliasId > 0) {
			msg.setUnionAliasId(unionAliasId);
		}
		msg.setVipExp(vipExp);
		msg.setSex(sex);
		if(null != earnSpeed){
			msg.setEarnSpeed(earnSpeed.toString());
		}
		if(registerTime > 0){
			msg.setRegisterTime(registerTime);
		}
		if(serverId > 0){
			msg.setServerId(serverId);
		}
		if(null != world){
			msg.setWorld(world);
		}
		if(null != continent){
			msg.setContinent(continent);
		}
		if(null != country){
			msg.setCountry(country);
		}
		if(null != province){
			msg.setProvince(province);
		}
		if(null != city){
			msg.setCity(city);
		}
		if(lastLoginTime > 0){
			msg.setLastLoginTime(lastLoginTime);
		}
		msg.setBadge(badge);
		return msg;
	}

	public static UserBaseInfo parseBaseUserInfo(PlayerProto.PlayerBaseTempMsg msg){
		UserBaseInfo userBaseInfo = new UserBaseInfo();
		userBaseInfo.setUserAliasId(msg.getUserAliasId());
		userBaseInfo.setNickName(msg.getNickName());
		userBaseInfo.setTitleId(msg.getTitleId());
		userBaseInfo.setDecoration(msg.getDecoration());
		userBaseInfo.setUnionAliasId(msg.getUnionAliasId());
		userBaseInfo.setUnionUid(msg.getUnionUid());
		userBaseInfo.setUnionName(msg.getUnionName());
		userBaseInfo.setVipExp(msg.getVipExp());
		userBaseInfo.setSex(msg.getSex());
		userBaseInfo.setEarnSpeed(msg.hasEarnSpeed() ? new BigInteger(msg.getEarnSpeed()) : BigInteger.ZERO);
		userBaseInfo.setRegisterTime(msg.getRegisterTime());
		userBaseInfo.setServerId(msg.getServerId());
		userBaseInfo.setWorld(msg.getWorld());
		userBaseInfo.setContinent(msg.getContinent());
		userBaseInfo.setCountry(msg.getCountry());
		userBaseInfo.setProvince(msg.getProvince());
		userBaseInfo.setCity(msg.getCity());
		userBaseInfo.setLastLoginTime(msg.getLastLoginTime());
		userBaseInfo.setHideVipAndAddress(msg.getHideVipAndAddress());
		if (msg.hasPrestige()) {
			String prestigeStr = msg.getPrestige();
			if (!StringUtils.isNullOrEmpty(prestigeStr)) {
				userBaseInfo.setPrestige(Long.parseLong(prestigeStr));
			}
		}
		userBaseInfo.setTotalAbility(msg.hasTotalAbility() ? new BigInteger(msg.getTotalAbility()) : BigInteger.ZERO);
		userBaseInfo.setTotalIntimacy(msg.hasTotalIntimacy() ? new BigInteger(msg.getTotalIntimacy()) : BigInteger.ZERO);
		userBaseInfo.setBadge(msg.getBadge());
		return userBaseInfo;
	}

	public static PlayerProto.PlayerBaseTempMsg.Builder parsePlayerBaseTempMsg(UserBaseInfo userBaseInfo){
		PlayerProto.PlayerBaseTempMsg.Builder msg = PlayerProto.PlayerBaseTempMsg.newBuilder();
		msg.setUserAliasId(userBaseInfo.getUserAliasId());
		msg.setNickName(userBaseInfo.getNickName());
		msg.setTitleId(userBaseInfo.getTitleId());
		msg.setDecoration(userBaseInfo.getDecoration());
		msg.setUnionName(userBaseInfo.getUnionName());
		msg.setUnionUid(userBaseInfo.getUnionUid());
		msg.setUnionAliasId(userBaseInfo.getUnionAliasId());
		msg.setVipExp(userBaseInfo.getVipExp());
		msg.setSex(userBaseInfo.getSex());
		msg.setEarnSpeed(userBaseInfo.getEarnSpeed().toString());
		msg.setRegisterTime(userBaseInfo.getRegisterTime());
		msg.setServerId(userBaseInfo.getServerId());
		msg.setWorld(userBaseInfo.getWorld());
		msg.setContinent(userBaseInfo.getContinent());
		msg.setCountry(userBaseInfo.getCountry());
		msg.setProvince(userBaseInfo.getProvince());
		msg.setCity(userBaseInfo.getCity());
		msg.setLastLoginTime(userBaseInfo.getLastLoginTime());
		msg.setPrestige(Long.valueOf(userBaseInfo.getPrestige()).toString());
		msg.setHideVipAndAddress(userBaseInfo.getHideVipAndAddress());
		msg.setTotalIntimacy(userBaseInfo.getTotalIntimacy().toString());
		msg.setTotalAbility(userBaseInfo.getTotalAbility().toString());
		msg.setUnionHonerPoint(userBaseInfo.getHonors());
		msg.setBadge(userBaseInfo.getBadge());
		return msg;
	}

	public static PlayerProto.PlayerSimpleBaseTempMsg.Builder parsePlayerSimpleBaseTempMsg(UserBaseInfo userBaseInfo){
		PlayerProto.PlayerSimpleBaseTempMsg.Builder msg = PlayerProto.PlayerSimpleBaseTempMsg.newBuilder();
		msg.setNickName(userBaseInfo.getNickName());
		msg.setDecoration(userBaseInfo.getDecoration());
		msg.setUnionName(userBaseInfo.getUnionName());
		msg.setUnionUid(userBaseInfo.getUnionUid());
		msg.setSex(userBaseInfo.getSex());
		return msg;
	}

	public static PlayerProto.PlayerBaseTempMsg.Builder parseUpdatePlayerBaseTempMsg(UserBaseInfo oldInfo, UserBaseInfo newInfo) {
		PlayerProto.PlayerBaseTempMsg.Builder msg = PlayerProto.PlayerBaseTempMsg.newBuilder();
		if (!Objects.equals(oldInfo.getUserAliasId(), newInfo.getUserAliasId())) msg.setUserAliasId(newInfo.getUserAliasId());
		if (!Objects.equals(oldInfo.getNickName(), newInfo.getNickName())) msg.setNickName(newInfo.getNickName());
		if (!Objects.equals(oldInfo.getTitleId(), newInfo.getTitleId())) msg.setTitleId(newInfo.getTitleId());
		if (!Objects.equals(oldInfo.getDecoration(), newInfo.getDecoration())) msg.setDecoration(newInfo.getDecoration());
		if (!Objects.equals(oldInfo.getUnionName(), newInfo.getUnionName())) msg.setUnionName(newInfo.getUnionName());
		if (!Objects.equals(oldInfo.getUnionUid(), newInfo.getUnionUid())) msg.setUnionUid(newInfo.getUnionUid());
		if (!Objects.equals(oldInfo.getUnionAliasId(), newInfo.getUnionAliasId())) msg.setUnionAliasId(newInfo.getUnionAliasId());
		if (!Objects.equals(oldInfo.getVipExp(), newInfo.getVipExp())) msg.setVipExp(newInfo.getVipExp());
		if (!Objects.equals(oldInfo.getSex(), newInfo.getSex())) msg.setSex(newInfo.getSex());
		if (!Objects.equals(oldInfo.getEarnSpeed(), newInfo.getEarnSpeed())) msg.setEarnSpeed(newInfo.getEarnSpeed().toString());
		if (!Objects.equals(oldInfo.getRegisterTime(), newInfo.getRegisterTime())) msg.setRegisterTime(newInfo.getRegisterTime());
		msg.setServerId(newInfo.getServerId()); // 服务器ID很重要别省
		if (!Objects.equals(oldInfo.getWorld(), newInfo.getWorld())) msg.setWorld(newInfo.getWorld());
		if (!Objects.equals(oldInfo.getContinent(), newInfo.getContinent())) msg.setContinent(newInfo.getContinent());
		if (!Objects.equals(oldInfo.getCountry(), newInfo.getCountry())) msg.setCountry(newInfo.getCountry());
		if (!Objects.equals(oldInfo.getProvince(), newInfo.getProvince())) msg.setProvince(newInfo.getProvince());
		if (!Objects.equals(oldInfo.getCity(), newInfo.getCity())) msg.setCity(newInfo.getCity());
		if (!Objects.equals(oldInfo.getLastLoginTime(), newInfo.getLastLoginTime())) msg.setLastLoginTime(newInfo.getLastLoginTime());
		if (!Objects.equals(oldInfo.getPrestige(), newInfo.getPrestige())) msg.setPrestige(String.valueOf(newInfo.getPrestige()));
		if (!Objects.equals(oldInfo.getHideVipAndAddress(), newInfo.getHideVipAndAddress())) msg.setHideVipAndAddress(newInfo.getHideVipAndAddress());
		if (!Objects.equals(oldInfo.getTotalIntimacy(), newInfo.getTotalIntimacy())) msg.setTotalIntimacy(String.valueOf(newInfo.getTotalIntimacy()));
		if (!Objects.equals(oldInfo.getTotalAbility(), newInfo.getTotalAbility())) msg.setTotalAbility(String.valueOf(newInfo.getTotalAbility()));
		if (!Objects.equals(oldInfo.getBadge(), newInfo.getBadge())) msg.setBadge(newInfo.getBadge());
		return msg;
	}

	public static UserBaseInfo getUpdateUserBaseInfo(UserBaseInfo oldInfo, PlayerProto.PlayerBaseTempMsg updateMsg) {
		UserBaseInfo newInfo = new UserBaseInfo();
		newInfo.setUserAliasId(updateMsg.hasUserAliasId() ? updateMsg.getUserAliasId() : oldInfo.getUserAliasId());
		newInfo.setNickName(updateMsg.hasNickName() ? updateMsg.getNickName() : oldInfo.getNickName());
		newInfo.setTitleId(updateMsg.hasTitleId() ? updateMsg.getTitleId() : oldInfo.getTitleId());
		newInfo.setDecoration(updateMsg.hasDecoration() ? updateMsg.getDecoration() : oldInfo.getDecoration());
		newInfo.setUnionAliasId(updateMsg.hasUnionAliasId() ? updateMsg.getUnionAliasId() : oldInfo.getUnionAliasId());
		newInfo.setUnionUid(updateMsg.hasUnionUid() ? updateMsg.getUnionUid() : oldInfo.getUnionUid());
		newInfo.setUnionName(updateMsg.hasUnionName() ? updateMsg.getUnionName() : oldInfo.getUnionName());
		newInfo.setVipExp(updateMsg.hasVipExp() ? updateMsg.getVipExp() : oldInfo.getVipExp());
		newInfo.setSex(updateMsg.hasSex() ? updateMsg.getSex() : oldInfo.getSex());
		newInfo.setEarnSpeed(updateMsg.hasEarnSpeed() ? new BigInteger(updateMsg.getEarnSpeed()) : oldInfo.getEarnSpeed());
		newInfo.setRegisterTime(updateMsg.hasRegisterTime() ? updateMsg.getRegisterTime() : oldInfo.getRegisterTime());
		newInfo.setServerId(updateMsg.hasServerId() ? updateMsg.getServerId() : oldInfo.getServerId());
		newInfo.setWorld(updateMsg.hasWorld() ? updateMsg.getWorld() : oldInfo.getWorld());
		newInfo.setContinent(updateMsg.hasContinent() ? updateMsg.getContinent() : oldInfo.getContinent());
		newInfo.setCountry(updateMsg.hasCountry() ? updateMsg.getCountry() : oldInfo.getCountry());
		newInfo.setProvince(updateMsg.hasProvince() ? updateMsg.getProvince() : oldInfo.getProvince());
		newInfo.setCity(updateMsg.hasCity() ? updateMsg.getCity() : oldInfo.getCity());
		newInfo.setLastLoginTime(updateMsg.hasLastLoginTime() ? updateMsg.getLastLoginTime() : oldInfo.getLastLoginTime());
		newInfo.setHideVipAndAddress(updateMsg.hasHideVipAndAddress() ? updateMsg.getHideVipAndAddress() : oldInfo.getHideVipAndAddress());
		newInfo.setPrestige(updateMsg.hasPrestige() ? Long.valueOf(updateMsg.getPrestige()) : oldInfo.getPrestige());
		newInfo.setTotalAbility(updateMsg.hasTotalAbility() ? new BigInteger(updateMsg.getTotalAbility()) : oldInfo.getTotalAbility());
		newInfo.setTotalIntimacy(updateMsg.hasTotalIntimacy() ? new BigInteger(updateMsg.getTotalIntimacy()) : oldInfo.getTotalIntimacy());
		newInfo.setHonors(updateMsg.getUnionHonerPoint());
		newInfo.setBadge(updateMsg.getBadge());
		return newInfo;
	}

	public static UserBaseInfo parseToUserBaseInfo(PlayerProto.PlayerBaseTempMsg msg){
		UserBaseInfo userBaseInfo = new UserBaseInfo();
		userBaseInfo.setUserAliasId(msg.getUserAliasId());
		userBaseInfo.setNickName(msg.getNickName());
		userBaseInfo.setTitleId(msg.getTitleId());
		userBaseInfo.setDecoration(msg.getDecoration());
		userBaseInfo.setUnionName(msg.getUnionName());
		userBaseInfo.setUnionUid(msg.getUnionUid());
		userBaseInfo.setUnionAliasId(msg.getUnionAliasId());
		userBaseInfo.setVipExp(msg.getVipExp());
		userBaseInfo.setSex(msg.getSex());
		userBaseInfo.setEarnSpeed(msg.hasEarnSpeed() ? new BigInteger(msg.getEarnSpeed()) : BigInteger.ZERO);
		userBaseInfo.setRegisterTime(msg.getRegisterTime());
		userBaseInfo.setServerId(msg.getServerId());
		userBaseInfo.setContinent(msg.getContinent());
		userBaseInfo.setCountry(msg.getCountry());
		userBaseInfo.setProvince(msg.getProvince());
		userBaseInfo.setCity(msg.getCity());
		userBaseInfo.setLastLoginTime(msg.getLastLoginTime());
		userBaseInfo.setHonors(msg.getUnionHonerPoint());
		if(!StringUtils.isNullOrEmpty(msg.getPrestige())) {
			userBaseInfo.setPrestige(Long.valueOf(msg.getPrestige()));
		}
		userBaseInfo.setHideVipAndAddress(msg.getHideVipAndAddress());
		userBaseInfo.setTotalAbility(msg.hasTotalAbility() ? new BigInteger(msg.getTotalAbility()) : BigInteger.ZERO);
		userBaseInfo.setTotalIntimacy(msg.hasTotalIntimacy() ? new BigInteger(msg.getTotalIntimacy()) : BigInteger.ZERO);
		userBaseInfo.setBadge(msg.getBadge());
		return userBaseInfo;
	}

	public static PlayerProto.HistoryAttributeMsg.Builder parseHistoryAttributeMsg(UserHistoryAttribute userHistoryAttribute){
		if(null == userHistoryAttribute){
			return null;
		}
		PlayerProto.HistoryAttributeMsg.Builder msg = PlayerProto.HistoryAttributeMsg.newBuilder();
		msg.setAbility(userHistoryAttribute.getAbility().toString());
		msg.setEarnSpeed(userHistoryAttribute.getEarnSpeed().toString());
		msg.setActivityStartAbility(userHistoryAttribute.getActivityStartAbility().toString());
		msg.setActivityStartEarnSpeed(userHistoryAttribute.getActivityStartEarnSpeed().toString());
		msg.setQualification(userHistoryAttribute.getQualification());
		msg.setActivityStartQualification(userHistoryAttribute.getActivityStartQualification());
		msg.setCrossActivityStartAbility(userHistoryAttribute.getCrossActivityStartAbility().toString());
		msg.setCrossActivityStartEarnSpeed(userHistoryAttribute.getCrossActivityStartEarnSpeed().toString());
		msg.setCrossActivityStartQualification(userHistoryAttribute.getCrossActivityStartQualification());
		return msg;
	}

	public static PlayerProto.AbilityTempMsg.Builder parseAbilityTempMsg(BigInteger totalAbility){
		PlayerProto.AbilityTempMsg.Builder msg = PlayerProto.AbilityTempMsg.newBuilder();
		if(null == totalAbility){
			msg.setTotalAbility("0");
		}else {
			msg.setTotalAbility(totalAbility.toString());
		}
		return msg;
	}

	public static PlayerProto.InteractiveRecordTempMsg.Builder parseInteractiveRecordTempMsg(InteractiveRecord interactiveRecord){
		if(null == interactiveRecord){
			return null;
		}
		PlayerProto.InteractiveRecordTempMsg.Builder msg = PlayerProto.InteractiveRecordTempMsg.newBuilder();
		msg.setRecordUid(interactiveRecord.getRecordUid());
		msg.setInteractiveType(interactiveRecord.getInteractiveType());
		msg.setOperateUserId(interactiveRecord.getOperateUserId());
		PlayerProto.PlayerBaseTempMsg.Builder baseMsg = parsePlayerBaseTempMsg(interactiveRecord.getOperateBaseInfo());
		msg.setPlayerBaseData(baseMsg);
		msg.setRecordTime(interactiveRecord.getRecordTime());
		return msg;
	}

	public static PlayerProto.InteractiveRecordListMsg.Builder parseInteractiveRecordListMsg(List<InteractiveRecord> interactiveRecordList){
		PlayerProto.InteractiveRecordListMsg.Builder listMsg = PlayerProto.InteractiveRecordListMsg.newBuilder();
		if(null != interactiveRecordList){
			for(InteractiveRecord interactiveRecord : interactiveRecordList){
				PlayerProto.InteractiveRecordTempMsg.Builder msg = parseInteractiveRecordTempMsg(interactiveRecord);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}

	public static PlayerProto.InteractiveDetailMsg.Builder parseInteractiveDetailMsg(UserInteractive userInteractive){
		if(null == userInteractive){
			return null;
		}
		PlayerProto.InteractiveDetailMsg.Builder msg = PlayerProto.InteractiveDetailMsg.newBuilder();
		List<Long> likeUserIdList = StringUtils.stringToLongList(userInteractive.getLikeUserIdListString(), ",");
		List<Long> hateUserIdList = StringUtils.stringToLongList(userInteractive.getHateUserIdListString(), ",");
		msg.addAllLikeUserIdList(likeUserIdList);
		msg.addAllHateUserIdList(hateUserIdList);
		return msg;
	}

	public static PlayerProto.InteractiveNoticeMsg.Builder parseInteractiveNoticeMsg(InteractiveRecord interactiveRecord, BigInteger effectValue){
		PlayerProto.InteractiveNoticeMsg.Builder msg = PlayerProto.InteractiveNoticeMsg.newBuilder();
		msg.setInteractiveData(parseInteractiveRecordTempMsg(interactiveRecord));
		msg.setEffectValue(effectValue.toString());
		return msg;
	}
}
